﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using HotelManagement.DTO;

namespace HotelManagement.DAO
{
    class RoomTypeDAO:DataProvider
    {
        public RoomTypeDAO()
            :base()
        {

        }

        public bool Insert(RoomTypeDTO roomType)
        {
            string strInsert = "insert into roomtype(RoomTypeName,Price,Describe) values (";
            strInsert += "N'" + roomType.RoomTypeName + "',";
            strInsert += roomType.Price + ",";
            strInsert += "N'" + roomType.Describe + "')";

            return base._ExecuteQuery(strInsert);
        }

        public bool Update(RoomTypeDTO roomType)
        {
            string strUpdate = "update roomType set ";
            strUpdate += "roomType.RoomTypeName = N'" + roomType.RoomTypeName + "',";
            strUpdate += "roomType.Price = " + roomType.Price + ",";
            strUpdate += "roomType.Describe = N'" + roomType.Describe + "' where roomType.RoomTypeID = " + roomType.RoomTypeID;

            return base._ExecuteQuery(strUpdate);
        }

        public DataTable Select()
        {
            string strSelect = "select * from roomtype";

            return base._Select(strSelect);
        }
        /// <summary>
        /// Hàm này dùng để đọc ra một Loại Phòng từ một mã số của loại phòng đó
        /// </summary>
        /// <param name="pId"></param>
        /// <returns></returns>
        public RoomTypeDTO SelectARoomType(int pId)
        {
            if (pId<=0 || pId==null)
            {
                return null;
            }
            string sql = "select roomtype.RoomTypeName,roomtype.Price,roomtype.Describe from roomtype where roomtype.RoomTypeID=" + pId;

            DataTable tbl = base._Select(sql);
            if (tbl.Rows.Count<=0)
            {
                return null;
            }
            RoomTypeDTO roomT = new RoomTypeDTO();
            roomT.RoomTypeID = pId;
            roomT.RoomTypeName = (tbl.Rows[0]["RoomTypeName"]).ToString();
            roomT.Price = float.Parse((tbl.Rows[0]["Price"]).ToString());
            roomT.Describe = (tbl.Rows[0]["Describe"]).ToString();
            return roomT;
        }

        public int SelectLastInsertID()
        {
            return base.SelectLastInsertID("RoomType", "RoomTypeID");
        }
    }
}
